package com.game.jumper.lghelp;

import java.util.Random;

import org.loon.framework.android.game.core.graphics.LImage;
import org.loon.framework.android.game.core.graphics.device.LGraphics;

import android.graphics.Bitmap;
import android.graphics.Rect;

import com.game.jumper.activity.PlayLG;
import com.game.jumper.game.Constant;

public class Lg_Back implements ILgHelp
{

	private LImage[] leaf = new LImage[3];

	private Random random = new Random();

	// private LImage[] images=new LImage[9];

	private LImage img;

	int[] padxs;

	public Lg_Back()
	{
		leaf[0] = LImage.createImage("assets/leaf.png");
		leaf[1] = LImage.createImage("assets/leaf.png");
		leaf[2] = LImage.createImage("assets/leaf.png");
		padxs = new int[] { random.nextInt((int) Constant.Width) - (int) Constant.Width / 2,
				random.nextInt((int) Constant.Width) - (int) Constant.Width / 2,
				random.nextInt((int) Constant.Width) - (int) Constant.Width / 2 };
	}

	double[] ts = new double[] { 0, Constant.HIGHT * 2, Constant.HIGHT * 4 };

	int n = 0;

	@Override
	public void draw(LGraphics g)
	{
		// TODO Auto-generated method stub
		Bitmap image = PlayLG.backimage[n / 3];
		g.drawBitmap(image, new Rect(0, 0, image.getWidth(), image.getHeight()),
				new Rect(0, 0, g.getClipWidth(), g.getClipHeight()));

		// g.drawImage(img, 0, 0,(int)Constant.Width,(int)Constant.HIGHT);
		n++;
		if (n / 3 > 8)
			n = 0;
		for (int i = 0; i < ts.length; i++)
		{
			ts[i] += random.nextInt(5) + 2;
			drawleaf(ts[i], g, leaf[i], padxs[i]);
			if (ts[i] > Constant.HIGHT * 6)
				ts[i] = 0;
		}

	}

	private void drawleaf(double t, LGraphics g, LImage img, int padx)
	{

		double x = 0.5 * t * Math.cos(t * Math.PI / 180) + Constant.Width * 0.3 + padx;
		double y = t;
		g.drawImage(img, (int) x, (int) y);

	}

	/**
	 * 坐标系转换
	 * 
	 * @param p
	 * @param thta
	 * @return
	 */
	private double[] rtoxy(double r, double thta)
	{
		double x = r * Math.cos(thta);
		double y = r * Math.sin(thta);
		return new double[] { x, y };
	}
}
